package com.goldenautumn.prefabricate.activity.mine.adapter

import android.content.Context
import android.view.ViewGroup
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.ImageSelectVo
import com.goldenautumn.prefabricate.databinding.ItemImageSelectBinding
import com.goldenautumn.prefabricate.utils.loadImgUrl
import qncx.external.common.compat.gone
import qncx.external.common.compat.visible

class ImgSelectAdapter : BaseMultiItemAdapter<ImageSelectVo>() {

    companion object {
        private const val ITEM_TYPE_LINE = 1
        private const val ITEM_TYPE_GRID = 2
    }

    init {
        addItemType(ITEM_TYPE_LINE, object : OnMultiItemAdapterListener<ImageSelectVo, DataBindingHolder<ItemImageSelectBinding>> {

            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemImageSelectBinding> {
                return DataBindingHolder(R.layout.item_image_select,parent)
            }

            override fun onBind(holder: DataBindingHolder<ItemImageSelectBinding>, position: Int, item: ImageSelectVo?) {
                // 绑定 item 数据
                if(item == null) return
                var mBinding  = holder.binding
                mBinding.shapeLinearLayout6.visible()
                mBinding.img.gone()
                mBinding.subBtn.gone()
            }

        }).addItemType(ITEM_TYPE_GRID, object : OnMultiItemAdapterListener<ImageSelectVo, DataBindingHolder<ItemImageSelectBinding>> {

            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemImageSelectBinding> {
                return DataBindingHolder(R.layout.item_image_select,parent)
            }

            override fun onBind(holder: DataBindingHolder<ItemImageSelectBinding>, position: Int, item: ImageSelectVo?) {
                // 绑定 item 数据
                if(item == null) return
                var mBinding  = holder.binding
                mBinding.shapeLinearLayout6.gone()
                mBinding.img.visible()
                mBinding.subBtn.visible()
                mBinding.img.loadImgUrl(if(item?.img?.realPath?.contains("/storage/emulated/0") == true) item?.img?.realPath else item?.img?.path,16)
            }

        }).onItemViewType { position, list ->
            if (list[position]?.type == 1) ITEM_TYPE_LINE
            else ITEM_TYPE_GRID
        }
    }

    override fun getItemCount(items: List<ImageSelectVo>): Int {
        return  return if(items.size > 9) 9 else super.getItemCount(items)
    }

}